﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Openfree.Framework.DataAccess;
using System.Data.SqlClient;

public partial class GoodBackManager : PMSBase
{
    string cmdText = "SELECT T.*,(select UserName from T_User where UserID=T.CreateUser) as CreateUserName" + PMSAccess.AppendSql_UserOrg + " FROM {0} T where 1=1 ";
    protected void Page_Load(object sender, EventArgs e)
    { 
        this.TableName = "IC_GoodBack";
        this.Key = "GoodBackID";
        if (!IsPostBack)
        {
            pnlGrid.Visible = true;
            pnlEdit.Visible = false;
            string sql = string.Format(cmdText + " ORDER BY T.CREATETIME DESC ", this.TableName);
            FillGirdView(0, sql);
            InitLoadData();
            if (!string.IsNullOrEmpty(Request["GoodBackID"]))
            {
                this.btnView_Click(null, e);
            }
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }
    private void InitLoadData()
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        StringBuilder sql = new StringBuilder(string.Format(cmdText, this.TableName));
        if (!string.IsNullOrEmpty(S_GoodBackCode.Text))
        {
            sql.Append(" AND T.GoodBackCode like '%").Append(S_GoodBackCode.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_GoodBackDate.Text))
        {
            sql.Append(" AND T.GoodBackDate = '").Append(S_GoodBackDate.Text).Append("'");
        }
        sql.Append(" ORDER BY T.CREATETIME DESC");
        FillGirdView(0, sql.ToString());
    }
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = false;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((HyperLink)e.Row.Cells[1].Controls[0]).NavigateUrl = "GoodBackManager.aspx?GoodBackID=" + ((HiddenField)e.Row.Cells[0].Controls[1]).Value;
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }
 

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        this.GoodBackItems.AddGoodBack();
    }


    protected void btnView_Click(object sender, EventArgs e)
    {
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        string backId = Request.Form["GoodBackID"];
        if(sender==null)
            backId = Request["GoodBackID"];
        if (string.IsNullOrEmpty(backId))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        this.GoodBackItems.ViewGoodBack(backId);
    }


    protected void btnRemove_Click(object sender, EventArgs e)
    {
        string backId = Request.Form["GoodBackID"];
        if (string.IsNullOrEmpty(backId))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        base.WriteLog("物料回收单", "删除物料回收单'" + backId + "'!",
            LogType.Warning, this.GetType().ToString());
        PMSAccess.RemoveDataRow(this.TableName, this.Key, backId);
        PMSAccess.RemoveDataRow("IC_GoodBackItem", this.Key, backId);
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }

}
